package com.xhu.dao.impl;

import com.xhu.bean.pojo.ChanceManage;
import com.xhu.dao.ChanceDao;
import com.xhu.utils.ConnectionFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ChanceDaoImpl implements ChanceDao {
    @Override
    public List<ChanceManage> queryPage(Integer start, Integer limit) {
        List<Object> parms=new ArrayList<>();

        StringBuffer sql=new StringBuffer(" select * from chance ");
        sql.append(" limit ?,? ");
        parms.add(start);
        parms.add(limit);

        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        List<ChanceManage> list=null;
        try {
            list=queryRunner.query(connection,sql.toString(),parms.toArray(),new BeanListHandler<>(ChanceManage.class));


        } catch (SQLException e) {
            System.out.println("list中的sql出现问题");
            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }


        return list;
    }

    @Override
    public int add(ChanceManage chanceManage) {
        String sql="INSERT INTO " +
                " chance(chance_code,chance_typename,des,cm_content," +
                " chance_name ,current_name,entry_name,cm_month,origin," +
                "operator_name,last_optime,assigner_name) " +
                " VALUE(?,?,?,?,?,?,?,?,?,?,?,?) ";
        Connection connection=ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        try {
            return queryRunner.update(connection,sql,
                    chanceManage.getChance_code(),
                    chanceManage.getChance_typename(),
                    chanceManage.getDes(),
                    chanceManage.getCm_content(),
                    chanceManage.getChance_name(),
                    chanceManage.getCurrent_name(),
                    chanceManage.getEntry_name(),
                    chanceManage.getCm_month(),
                    chanceManage.getOrigin(),
                    chanceManage.getOperator_name(),
                    chanceManage.getLast_optime(),
                    chanceManage.getAssigner_name());
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
    }

    @Override
    public int upData(ChanceManage chanceManage, String chance_id) {
        String sql=" UPDATE chance " +
                " set chance_code=?,chance_name=?,chance_typename=?,cm_content=?,des=? " +
                " where chance_id=? ";
        Connection connection=ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        try {
            return queryRunner.update(connection,sql,chanceManage.getChance_code(),
                    chanceManage.getChance_name(),
                    chanceManage.getChance_typename(),
                    chanceManage.getCm_content(),
                    chanceManage.getDes(),chance_id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
    }

    @Override
    public int del(String chance_id) {
        String sql=" DELETE FROM chance " +
                " WHERE chance_id=? ";
        Connection connection=ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        try {
            return queryRunner.update(connection,sql,chance_id);
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
    }

    @Override
    public int distribute(ChanceManage chanceManage, String chance_id) {
        String sql=" UPDATE chance " +
                " set chance_name=?,origin=?,chance_typename=?,entry_name=?,operator_name=?," +
                "cm_content=?,current_name=? " +
                " where chance_id=? ";
        Connection connection=ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        try {
            return queryRunner.update(connection,sql,chanceManage.getChance_name(),
                    chanceManage.getOrigin(),
                    chanceManage.getChance_typename(),
                    chanceManage.getEntry_name(),
                    chanceManage.getOperator_name(),
                    chanceManage.getCm_content(),
                    chanceManage.getCurrent_name(),
                    chance_id);
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
    }

    @Override
    public List<ChanceManage> queryPage(Integer start, Integer limit, String search_name) {
        List<Object> parms=new ArrayList<>();

        StringBuffer sql=new StringBuffer(" select * from chance where chance_code like '%" +search_name+ "%'");
        sql.append(" limit ?,? ");
        parms.add(start);
        parms.add(limit);

        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        List<ChanceManage> list=null;
        try {
            list=queryRunner.query(connection,sql.toString(),parms.toArray(),new BeanListHandler<>(ChanceManage.class));


        } catch (SQLException e) {
            System.out.println("list中的sql出现问题");
            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }


        return list;
    }
}
